package com.naiterui.ehp.ps.sms.service.impl;

import java.util.*;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.ps.sms.bean.Signature;
import com.naiterui.ehp.ps.sms.cache.SmsRamCache;
import com.naiterui.ehp.ps.sms.cache.SmsRedisManager;
import com.naiterui.ehp.ps.sms.dao.ISignatureDao;
import com.naiterui.ehp.ps.sms.exception.ExceptionCodes;
import com.naiterui.ehp.ps.sms.exception.SmsBusinessException;
import com.naiterui.ehp.ps.sms.service.ISignatureService;
import com.naiterui.ehp.ps.sms.utils.FilterHtml;
import com.naiterui.ehp.ps.sms.utils.constants.SmsConstants;
import com.naiterui.ehp.ps.sms.vo.SignatureVO;

import lombok.extern.slf4j.Slf4j;

/**
 * 部门信息service层实现
 *
 * @author
 * @since 1.0.0
 */
@Service
@Slf4j
public class SignatureServiceImpl implements ISignatureService {

    @Autowired
    private ISignatureDao signatureDao;

    @Override
    public List<SignatureVO> getAll() {
        List<Signature> signatureList = signatureDao.getAll();
        List<SignatureVO> signatureVOList = new ArrayList<SignatureVO>();
        for (Signature signature : signatureList) {
            SignatureVO signatureVO = new SignatureVO();
            BeanUtils.copyProperties(signature, signatureVO);
            signatureVO.setName(FilterHtml.xssProtect(signatureVO.getName()));
            signatureVOList.add(signatureVO);
        }
        return signatureVOList;
    }

    @Override
    public Map<Integer, Signature> getSignatureMap() {
        Map<Integer, Signature> sigantureMap = new HashMap<>();
        List<Signature> signatureList = signatureDao.getAll();
        for (Signature signature : signatureList) {
            sigantureMap.put(signature.getId(), signature);
        }
        return sigantureMap;
    }

    @Override
    public void addSignature(SignatureVO signatureVO) {
        Signature signature = new Signature();
        signature.setTitle(signatureVO.getTitle());
        signature.setName(signatureVO.getName());
        signature.setMontnetBusinessId(signatureVO.getMontnetBusinessId());
        signature.setCloopenTemplateId(signatureVO.getCloopenTemplateId());
        signature.setChannelNumber(signatureVO.getChannelNumber());
        signature.setIsDefault(signatureVO.getIsDefault());
        signature.setStatus((byte) SmsConstants.EFFECTIVE_STAUTS);// 默认有效
        signature.setCreatedAt(new Date());
        signatureDao.saveOrUpdate(signature);
        log.info("添加签名信息成功: {}", signature.toString());

        signatureVO.setId(signature.getId());
        SmsRamCache.setSignatureMapValue(signatureVO);
        SmsRedisManager.notifyUpdateSignature(signatureVO);
        log.info("添加签名信息缓存信息成功, signatureVO:  {}", signatureVO);
    }

    @Override
    public SignatureVO getSignatureByID(Integer id) {
        Signature signature = signatureDao.get(id);
        if (signature != null) {
            SignatureVO signatureVO = new SignatureVO();
            BeanUtils.copyProperties(signature, signatureVO);
            return signatureVO;
        }
        return null;
    }

    @Override
    public SignatureVO getDefaultSignature() {
        Signature signatureDefault = signatureDao.getDefault();
        if (signatureDefault == null) {
            return null;
        }
        SignatureVO signatureVO = new SignatureVO();
        BeanUtils.copyProperties(signatureDefault, signatureVO);
        return signatureVO;
    }

    @Override
    public void updateSignature(SignatureVO signatureVO) throws SmsBusinessException {
        Signature signature = signatureDao.get(signatureVO.getId());
        if (signature == null) {
            throw new SmsBusinessException(ExceptionCodes.PARAM_ERROR);
        }
        signature.setTitle(signatureVO.getTitle());
        signature.setName(signatureVO.getName());
        signature.setMontnetBusinessId(signatureVO.getMontnetBusinessId());
        signature.setCloopenTemplateId(signatureVO.getCloopenTemplateId());
        signature.setChannelNumber(signatureVO.getChannelNumber());
        signature.setChangedAt(new Date());
        signatureDao.saveOrUpdate(signature);

        SignatureVO signatureChangedVO = new SignatureVO();
        SmsRamCache.setSignatureMapValue(signatureChangedVO);
        SmsRedisManager.notifyUpdateSignature(signatureChangedVO);
        log.info("更新签名信息缓存信息成功, signatureVO: {}", signatureChangedVO);
    }

    @Override
    public void saveDefaultSignature(Integer id) throws SmsBusinessException {
        Signature signature = signatureDao.get(id);
        if (signature == null) {
            throw new SmsBusinessException(ExceptionCodes.PARAM_ERROR);
        }

        if (signature.getIsDefault() != null && SmsConstants.CODE_Y == signature.getIsDefault().intValue()) {
            return;
        }
        // 先设置已有的默认签名为非默认，然后将当前签名置为默认签名
        Signature signatureDefault = signatureDao.getDefault();
        if (signatureDefault != null) {
            signatureDefault.setIsDefault((byte) SmsConstants.CODE_N);
            signatureDefault.setChangedAt(new Date());
            signatureDao.saveOrUpdate(signatureDefault);

            SignatureVO defaultVO = new SignatureVO();
            BeanUtils.copyProperties(signatureDefault, defaultVO);
            SmsRamCache.setSignatureMapValue(defaultVO);
            SmsRedisManager.notifyUpdateSignature(defaultVO);
        }

        // 更新现有的签名为默认
        signature.setIsDefault((byte) SmsConstants.CODE_Y);
        signature.setChangedAt(new Date());
        signatureDao.saveOrUpdate(signature);

        SignatureVO newDefaultVO = new SignatureVO();
        BeanUtils.copyProperties(signature, newDefaultVO);
        SmsRamCache.setSignatureMapValue(newDefaultVO);
        SmsRedisManager.notifyUpdateSignature(newDefaultVO);
    }

    @Override
    public SignatureVO getSendSignatureFromCache(Integer id) {
        // 如果未配置签名，则直接获取默认签名
        if (id == null) {
            SignatureVO defaultVO = this.getDefaultSignature();
            return defaultVO;
        }
        SignatureVO signatureVO = SmsRamCache.getSignature(id);
        //如果缓存不存在，则从数据库回写
        if (signatureVO == null) {
            signatureVO = this.getSignatureByID(id);
            if (signatureVO != null) {
                SmsRamCache.setSignatureMapValue(signatureVO);
                SmsRedisManager.notifyUpdateSignature(signatureVO);
            }
        }
        return signatureVO;
    }

}
